home *** CD-ROM | disk | FTP | other *** search
/ The 640 MEG Shareware Studio 2 / The 640 Meg Shareware Studio CD-ROM Volume II (Data Express)(1993).ISO / clang / mcomm551.zip / ZMDOS.H < prev    next >
C/C++ Source or Header  |  1991-12-14  |  19KB  |  380 lines

  1.  
  2. /*/////////////////////////////////////////////////////////////////////
  3. //                                                                   //
  4. //  ZMDOS.H -- zmodem protocol header file                           //
  5. //                                                                   //
  6. //    Mike Dumdei, 6 Holly Lane, Texarkana TX, 75503                 //
  7. //                                                                   //
  8. //////////////////////////////////////////////////////////////////// */
  9. #ifdef __cplusplus
  10.   extern "C" {
  11. #endif
  12.  
  13. /*/////////////////////////////////////////////////////////
  14. //                                                       //
  15. //  general                                              //
  16. //                                                       //
  17. //////////////////////////////////////////////////////// */
  18. typedef unsigned char BYTE;
  19. typedef unsigned int WORD;
  20. typedef unsigned long DWORD;
  21.  
  22. /*/////////////////////////////////////////////////////////
  23. //                                                       //
  24. //  compiler specific                                    //
  25. //                                                       //
  26. //////////////////////////////////////////////////////// */
  27. #ifndef __TURBOC__
  28.   #ifndef __ZTC__
  29.     #define __MSC__
  30.   #endif
  31. #endif
  32.  
  33. #define MAX_PATH            148
  34. #define KBHIT               _bios_keybrd(1)
  35. #define KBREAD              _bios_keybrd(0)
  36. #define Upd16Crc(crc,c)     ((Crc16Tbl[(crc) >> 8]) ^ (((crc) << 8) ^ (c)))
  37. #define Upd32Crc(crc,c)     ((Crc32Tbl[((BYTE)(crc) ^ (c))]) ^ (((crc) >> 8)))
  38.  
  39. /*--------------------------------------------+
  40. |  DOS findfirst/findnext structure           |
  41. +--------------------------------------------*/
  42. typedef struct
  43. {
  44.     char reserved[21];
  45.     char attrib;
  46.     unsigned time;
  47.     unsigned date;
  48.     long size;
  49.     char name[13];
  50. } DF;
  51.  
  52. #ifdef __TURBOC__
  53.   #if (__TURBOC__ < 0x400)
  54.     #define _A_NORMAL           0
  55.     #define _A_RDONLY           FA_RDONLY
  56.     #define _A_SUBDIR           FA_DIREC
  57.     #define _A_HIDDEN           FA_HIDDEN
  58.     #define _A_SYSTEM           FA_SYSTEM
  59.     #define _A_VOLID            FA_LABEL
  60.   #endif
  61. #endif
  62.  
  63. #ifndef _MAX_PATH
  64.  #define _MAX_PATH 148
  65. #endif
  66.  
  67. /*/////////////////////////////////////////////////////////
  68. //                                                       //
  69. //  selected definitions from Chuck Forsberg's PD zmodem //
  70. //                                                       //
  71. //////////////////////////////////////////////////////// */
  72. /*--------------------------------------------+
  73. |  limits                                     |
  74. +--------------------------------------------*/
  75. #define ZATTNLEN    32        /* max length of attention string */
  76. #define MAXPACKET 1024        /* maximum size of a packet */
  77.  
  78. /*--------------------------------------------+
  79. |  special character definitions              |
  80. +--------------------------------------------*/
  81. #define ZPAD        '*'       /* pad char - begins zmodem frames */
  82. #define ZDLE      '\x18'      /* zmodem escape - aka CAN, Ctrl-X */
  83. #define ZBIN        'A'       /* binary frame indicator (CRC-16) */
  84. #define ZHEX        'B'       /* hex frame indicator */
  85. #define ZBIN32      'C'       /* binary frame indicator (CRC-32) */
  86. #define XON       '\x11'      /* XON character */
  87.  /*++ zdle sequences ++*/
  88. #define ZCRCE       'h'       /* CRC next, frame ends, hdr packet follows */
  89. #define ZCRCG       'i'       /* CRC next, frame continues nonstop */
  90. #define ZCRCQ       'j'       /* CRC next, frame continues, ZACK expected */
  91. #define ZCRCW       'k'       /* CRC next, ZACK expected, end of frame */
  92. #define ZRUB0       'l'       /* translate to DEL '\x7f' */
  93. #define ZRUB1       'm'       /* translate to DEL '\xff' */
  94. #define GOTCRCE (ZCRCE|256)   /* ZDLE-ZCRCE received */
  95. #define GOTCRCG (ZCRCG|256)   /* ZDLE-ZCRCG received */
  96. #define GOTCRCQ (ZCRCQ|256)   /* ZDLE-ZCRCQ received */
  97. #define GOTCRCW (ZCRCW|256)   /* ZDLE-ZCRCW received */
  98. #define GOTCAN     (256)      /* got 5 ZDLEs */
  99.  
  100. /*--------------------------------------------+
  101. |  zmodem frame types                         |
  102. +--------------------------------------------*/
  103. #define ZRQINIT      0        /* request receiver init */
  104. #define ZRINIT       1        /* receiver init */
  105. #define ZSINIT       2        /* sender init sequence (optional) */
  106. #define ZACK         3        /* zmodem ACK */
  107. #define ZFILE        4        /* file name from sender */
  108. #define ZSKIP        5        /* to sender: skip this file */
  109. #define ZNAK         6        /* zmodem NAK */
  110. #define ZABORT       7        /* abort batch transfers */
  111. #define ZFIN         8        /* finish session */
  112. #define ZRPOS        9        /* resume data trans at this position */
  113. #define ZDATA       10        /* data packet(s) follow */
  114. #define ZEOF        11        /* end of file */
  115. #define ZFERR       12        /* disk I/O error detected */
  116. #define ZCRC        13        /* request for file CRC and response */
  117. #define ZCHALLENGE  14        /* receiver's challenge */
  118. #define ZCOMPL      15        /* request is complete */
  119. #define ZCAN        16        /* other end canned session with CAN*5 */
  120. #define ZFREECNT    17        /* request for free bytes on filesystem */
  121. #define ZCOMMAND    18        /* command from sending program */
  122. #define ZSTDERR     19        /* output to standard error, data follows */
  123.  
  124. /*--------------------------------------------+
  125. |  Bit Masks for ZRINIT ZF0 flags             |
  126. +--------------------------------------------*/
  127. #define CANFDX     0x01       /* receiver can send and receive true FDX */
  128. #define CANOVIO    0x02       /* receiver can receive during disk I/O */
  129. #define CANBRK     0x04       /* receiver can send a break signal */
  130. #define CANCRY     0x08       /* receiver can decrypt */
  131. #define CANLZW     0x10       /* receiver can uncompress */
  132. #define CANFC32    0x20       /* receiver can use 32 bit Frame Check */
  133. #define ESCCTL     0x40       /* receiver expects ctl chars to be escaped */
  134. #define ESC8       0x80       /* receiver expects 8th bit to be escaped */
  135.  
  136. /*--------------------------------------------+
  137. |  Bit Masks for ZSINIT ZF0 flags             |
  138. +--------------------------------------------*/
  139. #define TESCCTL    0x40       /* tx expects ctl chars to be escaped */
  140. #define TESC8      0x80       /* tx expects 8th bit to be escaped */
  141.  
  142. /*--------------------------------------------+
  143. |  Bit masks for ZFILE header flags           |
  144. +--------------------------------------------*/
  145.  /*++ ZF0 flag, conversion options ++*/
  146. #define ZCBIN        1      /* binary transfer - inhibit conversion */
  147. #define ZCNL         2      /* convert NL to local end of line convention */
  148. #define ZCRESUM      3      /* resume interrupted file transfer */
  149.  /*++ ZF1 flag, management options ++*/
  150. #define ZMSKNOLOC  0x80     /* skip file if not present at rx */
  151. #define ZMMASK     0x1f     /* mask for the choices below */
  152. #define ZMNEWL       1      /* transfer if source newer or longer */
  153. #define ZMCRC        2      /* transfer if different file CRC or length */
  154. #define ZMAPND       3      /* append contents to existing file (if any) */
  155. #define ZMCLOB       4      /* replace existing file */
  156. #define ZMNEW        5      /* transfer if source newer */
  157. #define ZMDIFF       6      /* transfer if dates or lengths different */
  158. #define ZMPROT       7      /* protect destination file */
  159.  
  160. /*--------------------------------------------+
  161. |  Error return values                        |
  162. +--------------------------------------------*/
  163. #define GARB_COUNT     (-12)    /* header not found on call to ZRecvHdr */
  164. #define LONG_PKT       (-11)    /* exceeded expected block length */
  165. #define GARBLED_PKT    (-10)    /* unexpected data/invalid ZDLE sequence */
  166. #define BAD_CRC         (-9)    /* CRC incorrect */
  167. #define TIMED_OUT       (-8)    /* timed out on character receive */
  168.  
  169. #define SYNC_ERR        (-6)    /* couldn't synchronize file positions */
  170. #define MEM_ERR         (-5)    /* memory allocation error */
  171. #define FILE_ERR        (-4)    /* DOS read/write error */
  172. #define LOST_CARRIER    (-3)    /* lost carrier */
  173. #define REM_ABORT       (-2)    /* aborted by remote */
  174. #define LCL_ABORT       (-1)    /* aborted by local operator */
  175.  
  176. /*/////////////////////////////////////////////////////////
  177. //                                                       //
  178. //  zmodem definitions specific to this implementation   //
  179. //                                                       //
  180. //////////////////////////////////////////////////////// */
  181. /*--------------------------------------------+
  182. |  zmodem message types                       |
  183. +--------------------------------------------*/
  184. #define M_RHDR      0       /* received header */
  185. #define M_SHDR      1       /* sent header */
  186. #define M_BLK0      2       /* block 0 data processed (name, size, etc.) */
  187. #define M_CLASH     3       /* file name clash occurred (use ExistOpts) */
  188. #define M_FILE      4       /* start of transfer, FilePos = 1st position */
  189. #define M_EOF       5       /* end of transfer (1 file) */
  190. #define M_DATA      6       /* sent or received file data packet */
  191. #define M_FLOW      7       /* change in XOFF or CTS flow status */
  192. #define M_IDLE      8       /* waiting for character or for tx to empty */
  193. #define M_RESET     9       /* reset to 'first file' condition */
  194.  
  195. /*--------------------------------------------+
  196. |  zmodem header (i86 byte order dependent)   |
  197. +--------------------------------------------*/
  198. typedef union
  199. {
  200.     long Data;                  /* generic reference to all bytes */
  201.     long Pos;                   /* reference as a file position value */
  202.     WORD BufSz;                 /* receiver buffer size for ZRINIT */
  203.     BYTE Flg[4];                /* reference to individual flag values */
  204. } ZHEADER;
  205. enum zhdrflag { F3, F2, F1, F0 };
  206.  
  207. /*--------------------------------------------+
  208. |  bit mapped zmodem program control flags    |
  209. +--------------------------------------------*/
  210. typedef union
  211. {
  212.     int Data;                   /* reference to all bits at once */
  213.     struct                      /* reference to individual fields */
  214.     {
  215.         unsigned ExistOpts : 3; /* 0=skip, 1=resume, 2=makedup, 3=replace,
  216.                                    4=abort transfer */
  217.         unsigned KeepTime  : 1; /* use mod time in block 0 header */
  218.         unsigned EscCtl    : 1; /* escape txd ctl chars even if not req */
  219.         unsigned IgnCarrier: 1; /* ignore absence of carrier */
  220.         unsigned EndOfFile : 1; /* end of file encountered */
  221.         unsigned Receiving : 1; /* receiving if set, sending if reset */
  222.  
  223.         unsigned KBlocks   : 2; /* 0 = default (128 if X, 1024 if Y) */
  224.                                 /* 1 = force 128, 2 = force 1024 */
  225.         unsigned G         : 1; /* 1 = "g" mode, 0 = ACK mode */
  226.         unsigned CkSum     : 1; /* 1 = checksum block check, 0 = crc */
  227.         unsigned FullPath  : 1; /* 1 = send/accept complete path */
  228.     } F;
  229. } TFLAGS;
  230.  
  231. /*/////////////////////////////////////////////////////////
  232. //                                                       //
  233. //  async and timer specific functions                   //
  234. //                                                       //
  235. //////////////////////////////////////////////////////// */
  236. #define RxChar()          ((--RxBytesQd < 0) ? LoadRxQueue() : *RxQPtr++)
  237. #define FlushTxBuf()      async_txflush(tPort)
  238. #define FlushRxBuf()      RxBytesQd = 0, async_rxflush(tPort)
  239. #define TxChar(c)         async_tx(tPort, (c))
  240. #define TxBlock(blk, n)   async_txblk(tPort, (blk), (n))
  241. #define RxBufEmpty()      ((RxBytesQd + async_rxcnt(tPort)) == 0)
  242. #define Delay(t)          tdelay(t)
  243.  
  244. /*/////////////////////////////////////////////////////////
  245. //                                                       //
  246. //  parse file list structure/definitions                //
  247. //                                                       //
  248. //////////////////////////////////////////////////////// */
  249. /*--------------------------------------------+
  250. |  parse file list structure                  |
  251. +--------------------------------------------*/
  252. typedef struct
  253. {
  254.     char *lptr;             /* pointer to current positon in list */
  255.     DF findstruc;           /* dos findfirst/findnext structure */
  256.     char findatrib;         /* search attribute */
  257.     int flags;              /* control flags for ParseFileList function */
  258. } FILELIST;
  259.  
  260. /*--------------------------------------------+
  261. |  parse file list definitions                |
  262. +--------------------------------------------*/
  263. #define F_NOFILE      0     /* no matching files */
  264. #define F_RDWRITE     1     /* found file, has r/w attribute */
  265. #define F_RDONLY      2     /* found file, has read only attribute */
  266. #define F_SUBDIR      3     /* found subdirectory */
  267. #define F_SYSTEM      4     /* found hidden, system, or volid type file */
  268. #define F_NOWILD    0x01    /* error if list contains wild cards */
  269. #define F_NOLIST    0x02    /* error if list contains multiple names */
  270. #define F_NOSYS     0x04    /* don't find subdir, hidden, system, or volid */
  271. #define F_UNIQUE    0x08    /* return duplicate name if file exists */
  272. #define F_SLASH     0x10    /* separate pathnames with forward slashes */
  273. #define F_LOWER     0x20    /* return lower case filename */
  274. #define F_FIRST     0x40    /* NZ flag value if no other options used */
  275. #define F_REGFILE   (_A_NORMAL | _A_RDONLY)
  276. #define F_SYSMASK   (_A_SUBDIR | _A_HIDDEN | _A_SYSTEM | _A_VOLID)
  277.  
  278. /*/////////////////////////////////////////////////////////
  279. //                                                       //
  280. //  global variables                                     //
  281. //                                                       //
  282. //////////////////////////////////////////////////////// */
  283. /*--------------------------------------------+
  284. |  initialized global variables               |
  285. +--------------------------------------------*/
  286. extern char *DfltPath;          /* default download path */
  287. extern long ConnectBaud;        /* set to connect rate if using fixed link */
  288. extern int RxTimeOut;           /* receive char timeout in ticks (1/18 s.) */
  289. extern int DefRxTimeOut;        /* default value for RxTimeOut */
  290. extern int XonTimeOut;          /* max ticks before sending XON for RxChar */
  291. extern int XoffTimeOut;         /* max ticks before override for rx'd XOFF */
  292. extern int TxWindow;            /* max chars in transit, 0 if unlimited */
  293. extern int VBufSize;            /* size of variable disk buffer to use */
  294. extern TFLAGS TFlag;            /* transfer flags (resume,keeptime,ign car) */
  295. extern WORD Crc16Tbl[256];      /* 16 bit CRC table */
  296. extern BYTE ZTable[256];        /* used to speed up ZRxChar & ZTxChar */
  297.  
  298.  /*++ zrinit local receive buffer size, capability flags ++*/
  299. extern WORD BufSize;            /* 0 if overlaying I/O, else <= VBufSize */
  300. extern BYTE ZFR0;
  301. extern BYTE ZFR1;
  302.  
  303.  /*++ zsinit flags ++*/
  304. extern BYTE ZFS0;               /* don't need control characters esc'd */
  305.  
  306.  /*++ zfile management flags ++*/
  307. extern ZHEADER ZFMgt;               /* not requesting any file management */
  308. extern ZHEADER ZFMgtMask;           /* mask for incoming file mgt requests */
  309.  
  310. /*--------------------------------------------+
  311. |  uninitialized global variables             |
  312. +--------------------------------------------*/
  313. extern ASYNC *tPort;            /* pointer to port structure */
  314. extern char *PathName;          /* full pathname of current file */
  315. extern char *NameExt;           /* name and extension only of current file */
  316. extern long FilePos;            /* current file offset */
  317. extern long FileSize;           /* length of current file */
  318. extern long RxdFileSize;        /* file size from block 0 information */
  319. extern DWORD UnixTime;          /* current file time in Unix format */
  320. extern DWORD RxdUnixTime;       /* file time from block 0 information */
  321. extern WORD DosTime, DosDate;   /* current file time in DOS format */
  322. extern WORD DosAtrib;           /* file attribute of current file */
  323. extern long TotalBytes;         /* total bytes for all files */
  324. extern int TotalFiles;          /* total number of files */
  325. extern WORD SerNbr;             /* serial number of remote program */
  326. extern int BinHdr;              /* ZBIN32 if using CRC32, ZBIN if CRC16 */
  327. extern char rData32;            /* use 32 bit CRC for rx'd data packets */
  328. extern char tData32;            /* use 32 bit CRC for tx'd data packets */
  329. extern int BlkLen;              /* current block length */
  330. extern ZHEADER ZFOpts;          /* option flags received in last ZFILE hdr */
  331. extern ZHEADER ZHdr;            /* I/O buf for zmodem header data */
  332. extern WORD ErrCnt;             /* number of errors on current file */
  333. extern int BytesRxd;            /* # bytes in last data packet received */
  334. extern BYTE *IOBuf;             /* I/O buffer */
  335. extern BYTE RxQueue[128];       /* receive queue */
  336. extern BYTE *RxQPtr;            /* pointer to current RxQueue position */
  337. extern int RxBytesQd;           /* bytes in receive queue */
  338.  
  339. /*/////////////////////////////////////////////////////////
  340. //                                                       //
  341. //  function declarations                                //
  342. //                                                       //
  343. //////////////////////////////////////////////////////// */
  344. void   _C_ AsyncMode(int mode);
  345. WORD   _C_ Blk16Crc(BYTE *block, int bytes);
  346. int    _C_ BuildYZBlk0(void);
  347. void   _C_ CloseFile(void);
  348. int    _C_ DosFindFirst(char *pathname, int atrib, DF *fstruc);
  349. int    _C_ DosFindNext(DF *fstruc);
  350. long   _C_ DosGetDiskFree(int drive);
  351. int    _C_ DosSetFileTime(int handle, unsigned date, unsigned time);
  352. void   _C_ FlushDisk(void);
  353. char * _C_ GetNameExt(char *FullPath);
  354. int    _C_ GetTotalFiles(char *list, int flags);
  355. int    _C_ LoadRxQueue(void);
  356. int    _C_ OpenFile(char *name, char *mode);
  357. int    _C_ ParseFileList(FILELIST *f, int flags, ...);
  358. void   _C_ ParseYZBlk0(char *blk0);
  359. int    _C_ ReadDisk(int bytes);
  360. int    _C_ Seek(long pos);
  361. void   _C_ SendBreak(void);
  362. char * _C_ SkipChars(char *str);
  363. char * _C_ SkipSpaces(char *str);
  364. char * _C_ SwapChars(char *str, char find, char replace);
  365. int    _C_ WaitTxLevel(int level);
  366. int    _C_ WriteDisk(int bytes);
  367. int    _C_ XYInitVars(ASYNC *comport);
  368.  
  369. DWORD  _C_ DosToUnix(WORD time, WORD date);
  370. void   _C_ UnixToDos(WORD *time, WORD *date, DWORD unixtime);
  371.  
  372. int    _C_ ZmodemRecv(ASYNC *comport);
  373. int    _C_ ZmodemSend(ASYNC *comport, char *files);
  374. void   _C_ ZMsg(int type, ...);
  375.  
  376. #ifdef __cplusplus
  377.   }
  378. #endif
  379.  
  380.